<?php

namespace app\Services;

use Exception;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use function Hyperf\Config\config;

class QiniuService
{
    private $token;

    public function __construct()
    {
        $accessKey = config('qiniu.AccessKey');
        $secretKey = config('qiniu.SecretKey');
        $bucket = config('qiniu.Bucket');
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        $this->token = $token;
    }

    /**
     * 文件移动到七牛云
     * @param $fileName
     * @param $path
     * @return array
     * @throws Exception
     */
    public function moveToQiniu($fileName, $path): array
    {
        $file_dir = 'personal_project/'.date('Ymd').'/'; // 文件路径
        $file_dir_name = $file_dir.$fileName;
        // 文件上传
        $key = $file_dir_name;
        // 构建 UploadManager 对象
        $uploadMgr = new UploadManager();
        $result = $uploadMgr->putFile($this->token, $key, $path, null, 'application/octet-stream', true, null, 'v2');
        return $result ;
    }
}